//
//  EMailViewController.m
//  ZLMediaPlayer
//
//  Created by zhangliang on 2017/4/24.
//  Copyright © 2017年 zhangliang. All rights reserved.
//

#import "EMailViewController.h"

@interface EMailViewController ()

@end

@implementation EMailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Bug反馈";
    
    shareToMailButton = [self buttonWithFrame:CGRectMake(20, 100, 280, 40) action:@selector(btnClicked:) withTag:1000];
    [shareToMailButton setTitle:@"Bug反馈" forState:UIControlStateNormal];
}

- (void)btnClicked:(id)sender
{
     UIButton *btnSender = (UIButton *)sender;
     switch (btnSender.tag)
     {
             case 1000:  // 分享到邮箱
             {
                 [self showMailPicker];
                 break;
             }
             default:
                 break;
     }
}

 /*******************************************************************************
    55  * 方法名称：buttonWithFrame:action:
    56  * 功能描述：初始化页面按钮，并添加到页面视图
    57  * 输入参数：
    58  * 输出参数：
    59  ******************************************************************************/
- (UIButton *)buttonWithFrame:(CGRect)frame action:(SEL)action withTag:(int)tag
{
     UIImage *buttonBackgroundImage = [[UIImage imageNamed:@"button_background.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];
     UIImage *disabledButtonBackgroundImage = [[UIImage imageNamed:@"button_background_disabled.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:5];

     UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
     button.frame = frame;
     [button setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal];
     [button setBackgroundImage:disabledButtonBackgroundImage forState:UIControlStateDisabled];
     [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
     [button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
     [button addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
     button.tag = tag;
     [self.view addSubview:button];

     return button;
 }

- (void)showMailPicker
{
     Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
     if (mailClass !=nil) {
         if ([mailClass canSendMail]) {
             [self displayMailComposerSheet];
         } else {
             UIAlertController *alert= [UIAlertController alertControllerWithTitle:@"" message:@"设备不支持邮件功能" preferredStyle:UIAlertControllerStyleAlert];
             [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil]];
             [self presentViewController:alert animated:YES completion:nil];
        }
     } else { }
}

 - (void)displayMailComposerSheet
 {
         MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
    
         // 设置picker的委托方法，完成之后会自动调用成功或失败的方法
         picker.mailComposeDelegate = self;
         // 添加主题
         [picker setSubject:@"Bug反馈"];
         // 添加收件人
         NSArray *toRecipients = [NSArray arrayWithObject:@"279352257@qq.com"];
         // 说明：也可以添加多个收件人，代码如下所示：
     //    NSArray *toRecipients = [NSArray arrayWithObjects:@"one@qq.com",@"two@qq.com",nil];
         // 添加抄送
     //    NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@qq.com",@"third@qq.com", nil];
         // 添加密送
     //    NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@qq.com"];
    
         [picker setToRecipients:toRecipients];
     //    [picker setCcRecipients:ccRecipients];
     //    [picker setBccRecipients:bccRecipients];
         // 发送图片附件(其他格式的附件，可以都先转化称NSData类型，然后设置相应的mimeType即可，如txt类型为@"text/txt",doc类型为@"text/doc",pdf类型为@"file/pdf"等等)
         NSData *myData = [NSData dataWithContentsOfFile:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"feedback.png"]];
         [picker addAttachmentData:myData mimeType:@"image/jpeg" fileName:@"feedback.png"];
         NSString *emailBody = [NSString stringWithFormat:@"<p>文字没有全部显示</p>"];
    
         // 直接在HTML代码中写入图片的地址
     //    NSString *emailBody = [NSString stringWithFormat:@"<img src=/uploadfile/2014/0226/20140226093504351.com/&' /><p>我分享了图片</p>"];
    
         [picker setMessageBody:emailBody isHTML:YES];
         [self presentViewController:picker animated:YES completion:nil];
     }


- (void)mailComposeController:(MFMailComposeViewController*)controller
           didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
         switch (result)
         {
             case MFMailComposeResultCancelled:
                 NSLog(@"Result: Mail sending canceled");  // 邮件发送取消
                 break;
             case MFMailComposeResultSaved:
                 NSLog(@"Result: Mail saved");  // 邮件保存成功
                 break;
             case MFMailComposeResultSent:
                 NSLog(@"Result: Mail sent");  // 邮件发送成功
                 break;
             case MFMailComposeResultFailed:
                 NSLog(@"Result: Mail sending failed");  // 邮件发送失败
                 break;
             default:
                 NSLog(@"Result: Mail not sent");
                 break;
        }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    NSLog(@"messageComposeViewController");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
